package com.purong.purong.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.purong.purong.Contant;
import com.purong.purong.PuRongApp;
import com.purong.purong.R;
import com.purong.purong.bean.ConfigBean;
import com.purong.purong.bean.LoginBean;
import com.purong.purong.util.ACache;
import com.purong.purong.util.CommonUtil;
import com.purong.purong.util.DataCleanUtil;
import com.purong.purong.widget.ConfirmDialog;
import com.purong.purong.widget.RadiusImageView;
import com.yao.sdk.encrypt.MD5;
import com.yao.sdk.glide.GlideUtil;
import com.yao.sdk.net.ResponseInfo;

import java.util.LinkedHashMap;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by wenjiezeng on 2017/10/7.
 */
public class SettingActivity extends BaseActivity {
    private RadiusImageView avatar;
    private TextView userName,serviceTel,cacheSize;


    @Override
    public int getRootViewId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (PuRongApp.getLoginInfo(this)!=null){
            LoginBean bean=PuRongApp.getLoginInfo(this);
            findViewById(R.id.user_info_lay).setVisibility(View.VISIBLE);
            findViewById(R.id.reset_pwd).setVisibility(View.VISIBLE);
            findViewById(R.id.cut_line).setVisibility(View.VISIBLE);
            findViewById(R.id.login_out).setVisibility(View.VISIBLE);
            GlideUtil.getInstance().loadAvatar(this,bean.getAvatar(),avatar);
            userName.setText(bean.getNick());
        }else {
            findViewById(R.id.user_info_lay).setVisibility(View.GONE);
            findViewById(R.id.reset_pwd).setVisibility(View.GONE);
            findViewById(R.id.cut_line).setVisibility(View.GONE);
            findViewById(R.id.login_out).setVisibility(View.GONE);
            avatar.setImageResource(R.drawable.def_avatar);
            userName.setText("未设置");
        }
    }

    public void initView(){
        showTitle("设置");
        showBack();
        avatar=(RadiusImageView)findViewById(R.id.avatar);
        userName=(TextView)findViewById(R.id.user_name);
        serviceTel=(TextView)findViewById(R.id.service_tel);
        cacheSize=(TextView)findViewById(R.id.cacheSize);
        cacheSize.setText(DataCleanUtil.getTotalCacheSize(this));
    }

    @Override
    public void initData() {
        getServicePhone();
    }
    private void getServicePhone(){
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("config_code", "ServiceTel");
        request(Contant.CONFIG, params, true,false);
    }
    private void loginOut(){
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("device_no", MD5.getMD5Str(CommonUtil.getIMEI(this)));
        params.put("os", "1");
        params.put("version", CommonUtil.getVersionCode(this));
        request(Contant.LOGOUT, params, true,true);
    }

    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if (hasDestroy())
            return;
        super.onResponse(responseInfo);
        if (responseInfo.url.equals(Contant.LOGOUT)){
            if (responseInfo.isRequestOk){
                PuRongApp.getLoginInfo(this).remove(this);
                findViewById(R.id.login_out).setVisibility(View.GONE);
                JPushInterface.deleteAlias(this,0);
                showToast("已退出登录");
                this.finish();
            }

        }else if (responseInfo.url.equals(Contant.CONFIG)){
            if (responseInfo.isRequestOk){
                try{
                    ConfigBean bean= JSON.parseObject(responseInfo.content,ConfigBean.class);
                    if (bean!=null&&bean.getConfig()!=null&&bean.getConfig().getServiceTel()!=null){
                        serviceTel.setText(bean.getConfig().getServiceTel());
                    }
                }catch (Exception e){

                }
            }
        }
    }

    public void onClick(View v){
        switch (v.getId()){
            case R.id.clear_cache:
                ConfirmDialog clearDialog=new ConfirmDialog(this, "确定清除缓存？", new ConfirmDialog.onConfirmClickListenr() {
                    @Override
                    public void onConfirm() {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                ACache mCache= ACache.get(SettingActivity.this);
                                mCache.clear();
                                Glide.get(SettingActivity.this).clearDiskCache();
                                DataCleanUtil.clearAllCache(SettingActivity.this);

                            }
                        }).start();

                        cacheSize.setText("0.0KB");
                    }

                    @Override
                    public void onCancel() {

                    }
                });
                clearDialog.show();
                break;
            case R.id.suggest:
                Intent suggestIntent=new Intent(this,SuggestActivity.class);
                startActivity(suggestIntent);
                break;
            case R.id.user_info_lay:
                Intent infoIntent=new Intent(this,UserInfoActivity.class);
                startActivity(infoIntent);
                break;
            case R.id.login_out:
                ConfirmDialog loginOutDialog=new ConfirmDialog(this, "确定要退出登录？", new ConfirmDialog.onConfirmClickListenr() {
                    @Override
                    public void onConfirm() {
                        loginOut();
                    }

                    @Override
                    public void onCancel() {

                    }
                });
                loginOutDialog.show();

                break;
            case R.id.reset_pwd:
                Intent intent=new Intent(this,ReSetPwdActivity.class);
                startActivity(intent);
                break;
            case R.id.service_tel_lay:
                if (!serviceTel.getText().toString().trim().equals("")){
                    ConfirmDialog dialog=new ConfirmDialog(this, "确定拨打"+serviceTel.getText().toString().trim()+"?", new ConfirmDialog.onConfirmClickListenr() {
                        @Override
                        public void onConfirm() {
                            Intent callIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+serviceTel.getText().toString().trim()));
                            callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(callIntent);
                        }

                        @Override
                        public void onCancel() {

                        }
                    });
                    dialog.show();

                }
                break;
        }
    }

}
